package com.example.finalexam;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class DBManager {
    private DBHelper dbhelper;
    private SQLiteDatabase db;

    public DBManager(Context context)
    {
        dbhelper=new DBHelper(context);
        db=dbhelper.getWritableDatabase();
    }

    //添加单词
    public void add(List<Word> words) {
        db.beginTransaction();
        try {
            for (Word word : words) {
                db.execSQL("INSERT INTO word VALUES(null,?,?,?)", new Object[]{word.en_meaning, word.cn_meaning});
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
    //从数据库中将单词取出来
    public ArrayList<Word> getWords() {
        ArrayList<Word> words = new ArrayList<>();
        Cursor cursor =db.rawQuery("SELECT*FROM word", null);
        while (cursor.moveToNext()) {
            Word word = new Word();
            word.en_meaning = cursor.getString(cursor.getColumnIndex("en_meaning"));
            word.cn_meaning = cursor.getString(cursor.getColumnIndex("cn_meaning"));
            words.add(word);
    }
        cursor.close();
        db.close();
        return words;
}
    //取得数据库中的每行游标
    public Cursor queryTheCursor() {
        Cursor c=db.rawQuery("SELECT*FROM word", null);
        return c;
    }
    //清除所有单词方便下一次录入
    public void deleteAll(){
        String sql = "delete from word";
        db.execSQL(sql,new Object[]{,});
        db.close();
    }
}





